﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<div id="updateDialog" class="crudDialog">
	<form id="updateForm" method="post">
		<div class="form-group">
			<label for="infoId">编号</label>
			<input id="infoId" type="text" class="form-control" name="infoId" maxlength="20" value="${partInfo.infoId}" readonly>
		</div>
		<div class="form-group">
			<label for="model">型号</label>
			<input id="model" type="text" class="form-control" name="model" maxlength="32" value="${partInfo.model}">
		</div>
		<div class="form-group">
			<label for="spec">规格</label>
			<input id="spec" type="text" class="form-control" name="spec" maxlength="20" value="${partInfo.spec}">
		</div>
		<div class="form-group">
			<span>
				<select id="categoryId" name="categoryId">
					<option value="0" selected="selected">请选择分类</option>
					<c:forEach var="partCategorie" items="${partCategories}">
					<option value="${partCategorie.categoryId}">${partCategorie.name}</option>
					</c:forEach>
				</select>
			</span>
			<span class="type2 type3" hidden>
				<select id="pid" name="pid">
					<option value="0">请选择上级</option>
				</select>
			</span>
		</div>
		<div class="form-group">
			<label for="unit">单位</label>
			<input id="unit" type="text" class="form-control" name="unit" maxlength="20" value="${partInfo.unit}">
		</div>
		<div class="form-group">
			<label for="brank">品牌</label>
			<input id="brank" type="text" class="form-control" name="brank" maxlength="20" value="${partInfo.brank}">
		</div>
		<div class="form-group">
			<label for="price">单价</label>
			<input id="price" type="text" class="form-control" name="price" maxlength="20" value="${partInfo.price}">
		</div>
		<div class="form-group">
			<label for="cremark">备注</label>
			<input id="cremark" type="text" class="form-control" name="cremark" maxlength="50" value="${partInfo.cremark}">
		</div>
		<div class="form-group text-right dialog-buttons">
			<a class="waves-effect waves-button" href="javascript:;" onclick="createSubmit();">保存</a>
			<a class="waves-effect waves-button" href="javascript:;" onclick="createDialog.close();">取消</a>
		</div>
	</form>
</div>
<script>
$(function() {
	var categoryObj = $("#categoryId").select2();
	var optionVal = '${partInfo.categoryId}';
	categoryObj.val(optionVal).trigger("change");
	categoryObj.change();
});

function createSubmit() {
    $.ajax({
        type: 'post',
        url: '${basePath}/base/part/update/${partInfo.infoId}.do',
        data: $('#updateForm').serialize(),
        beforeSend: function() {
            if ($('#model').val() == '' ) {
                $('#model').focus();
                return false;
            }
            if ($('#spec').val() == '' ) {
                $('#spec').focus();
                return false;
            }
        },
        success: function(result) {
			if (result.code != 1) {
				if (result.data instanceof Array) {
					$.each(result.data, function(index, value) {
						$.confirm({
							animation: 'rotateX',
							closeAnimation: 'rotateX',
							title: false,
							content: value.errorMsg,
							buttons: {
								confirm: {
									text: '确认',
									btnClass: 'waves-effect waves-button waves-light'
								}
							}
						});
					});
				} else {
						$.confirm({
							theme: 'dark',
							animation: 'rotateX',
							closeAnimation: 'rotateX',
							title: false,
							content: result.data.errorMsg || result.data,
							buttons: {
								confirm: {
									text: '确认',
									btnClass: 'waves-effect waves-button waves-light'
								}
							}
						});
				}
			} else {
				updateDialog.close();
				$table.bootstrapTable('refresh');
			}
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
			$.confirm({
				theme: 'dark',
				animation: 'rotateX',
				closeAnimation: 'rotateX',
				title: false,
				content: textStatus,
				buttons: {
					confirm: {
						text: '确认',
						btnClass: 'waves-effect waves-button waves-light'
					}
				}
			});
        }
    });
}
</script>